<?php
function calculateContentPulseList(){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
	//Pulse menu for adding products, cats, etc
	?>
	<div>
		<a href="index.php?route=kse_admin/pulselist/add/&type=<?php echo $_GET["type"] ?>">Добавить</a>
	</div>
	<?php
	switch($_GET["type"]){
			case "products":
				echo "<form>
				<table class='sortable'>
					<thead>
						<tr>
							<th>Название</th>
							<th>Производитель</th>
							<th>Категория</th>
							<th>Цена</th>
							<th>Публ.</th>
							<th>Рекоменд.</th>
							<th>Удалить</th>
						</tr>
					</thead>
					<tbody>";
					$query = "SELECT product_id, product_name, (SELECT producer_name FROM kpulse_producers WHERE producer_id=product_producer_id) as producer, (SELECT cat_name FROM kpulse_categories WHERE cat_id=product_cat_id) as category, product_publicate, product_price, product_recomended FROM kpulse_products LIMIT 100";
					$result = mysql_query($query);
					while($row = mysql_fetch_assoc($result)){
						echo "<tr>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["product_id"] ."&type=". $_GET["type"] ."\">". $row["product_name"] ."</a></td>
						<td>". $row["producer"] ."</td>
						<td>". $row["category"] ."</td>
						<td>". $row["product_price"] ."</td>
						<td>";
						echo $row["product_publicate"] ? "<input type=\"checkbox\" name=\"publicate\" value=\"1\" checked=\"\">" : "<input type=\"checkbox\" name=\"publicate\" value=\"1\" >" ."</td>";
						echo "<td>";
						echo $row["product_recomended"] ? "<input type=\"checkbox\" name=\"recomended\" value=\"1\" checked=\"\">" : "<input type=\"checkbox\" name=\"recomended\" value=\"1\" >" ."</td>";
						echo "<td><a class=\"kse-menu-item\" onClick=\"kseDelelement()\"><img class=\"icons\" src=\"images/icons/delete.png\" /></a></td>
					</tr>";
					}
			break;
			case "categories":
				echo "<form>
				<table class='sortable'>
					<thead>
						<tr>
							<th>Название Категории</th>
							<th>Родительская</th>
							<th>Публикация</th>
							<th>Удалить</th>
						</tr>
					</thead>
					<tbody>";
					$query = "SELECT cat_id, cat_name, cat_visible, (SELECT cat_name FROM kpulse_categories WHERE cat_lft < t1.cat_lft AND cat_rgt > t1.cat_rgt AND cat_id > 1 ORDER BY cat_lft LIMIT 1 ) as parent FROM kpulse_categories as t1 LIMIT 100";
					$result = mysql_query($query);
					while($row = mysql_fetch_assoc($result)){
						echo "<tr>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["cat_id"] ."&type=". $_GET["type"] ."\">". $row["cat_name"] ."</td>
						<td>". $row["parent"] ."</td>
						<td>";
						echo $row["cat_visible"] ? "<input type=\"checkbox\" name=\"cat_visible\" value=\"1\" checked=\"\">" : "<input type=\"checkbox\" name=\"cat_visible\" value=\"1\" >" ."</td>";
						echo "<td><a class=\"kse-menu-item\" onClick=\"kseDelelement()\"><img class=\"icons\" src=\"images/icons/delete.png\" /></a></td>
					</tr>";
					}
			break;
			case "producers":
				echo "<form>
				<table class='sortable'>
					<thead>
						<tr>
							<th>Название Производителя</th>
							<th>Удалить</th>
						</tr>
					</thead>
					<tbody>";
					$query = "SELECT producer_id, producer_name FROM kpulse_producers LIMIT 100";
					$result = mysql_query($query);
					while($row = mysql_fetch_assoc($result)){
						echo "<tr>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["producer_id"] ."&type=". $_GET["type"] ."\">". $row["producer_name"] ."</td>
						<td><a class=\"kse-menu-item\" onClick=\"kseDelelement()\"><img class=\"icons\" src=\"images/icons/delete.png\" /></a></td>
						</tr>";
					}
			break;
		}
		echo "</tbody></table></form>";
	}
}

function calculateContentPulseView(){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		//print_r($_GET);exit();
		$submit = "<input id=\"submitbutton\" value=\"Edit\" type=\"button\" onClick=\"submit". $_GET["type"] ."()\"></div>";
		switch($_GET["type"]){
			case "products":
				$query = "SELECT *, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=product_creator_id) as creator, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=product_editor_id) as editor FROM kpulse_products WHERE product_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error());
									
				$row = mysql_fetch_assoc($result);
				$content = displayFormTemplatePulse($_GET["type"], $row);
			break;
			case "categories":
				$query = "SELECT *, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=cat_creator_id) as creator, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=cat_editor_id) as editor FROM kpulse_categories WHERE cat_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error());
									
				$row = mysql_fetch_assoc($result);
				$content = displayFormTemplatePulse($_GET["type"], $row);		
			break;
			case "producers":
				$query = "SELECT *, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=producer_creator_id) as creator, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=producer_editor_id) as editor FROM kpulse_producers WHERE producer_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error());
									
				$row = mysql_fetch_assoc($result);
				$content = displayFormTemplatePulse($_GET["type"], $row);		
			break;
		}
	}
}

function PulseAddContent($POSTVAR){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		switch($_GET["type"]){
			case "products":
				$POSTVAR["product_publicate"] = isset($POSTVAR["product_publicate"]) ? $POSTVAR["product_publicate"] : 0;
				$POSTVAR["product_recomended"] = isset($POSTVAR["product_recomended"]) ? $POSTVAR["product_recomended"] : 0;
				$POSTVAR["product_description_index"] = isset($POSTVAR["product_description_index"]) ? $POSTVAR["product_description_index"] : 0;
				
				$query = "INSERT INTO kpulse_products SET product_name='". addslashes($POSTVAR["product_name"]) ."', 
															product_code_number='". addslashes($POSTVAR["product_code_number"]) ."',
															product_producer_id=". $POSTVAR["product_producer_id"] .", 
															product_publicate=". $POSTVAR["product_publicate"] .", 
															product_recomended=". $POSTVAR["product_recomended"] .", 
															product_cat_id=". $POSTVAR["product_cat_id"] .",
															product_admin_rating=". $POSTVAR["product_admin_rating"] .", 
															product_price=". $POSTVAR["product_price"] .", 
															product_description='". addslashes($POSTVAR["product_description"]) ."',
															product_description_index=". $POSTVAR["product_description_index"] .",
															product_short_des='". addslashes($POSTVAR["product_short_des"]) ."',
															product_keywords='". addslashes($POSTVAR["product_keywords"]) ."',
															product_page_des='". addslashes($POSTVAR["product_page_des"]) ."',
															product_creator_id=". $_SESSION["user_id"] .",
														  	product_created=". time() .",
														  	product_editor_id=". $_SESSION["user_id"] .",
														  	product_edited=". time(); 
				
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				
				$query = "SELECT product_id FROM kpulse_products WHERE product_name='". addslashes($POSTVAR["product_name"]) ."'";
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				list($productid) = mysql_fetch_row($result);

				addPhotos($_FILES, $POSTVAR, $productid);
			break;
		}
	}
}

function PulseEditContent($POSTVAR){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		switch($_GET["type"]){
			case "products":
				$POSTVAR["product_publicate"] = isset($POSTVAR["product_publicate"]) ? $POSTVAR["product_publicate"] : 0;
				$POSTVAR["product_recomended"] = isset($POSTVAR["product_recomended"]) ? $POSTVAR["product_recomended"] : 0;
				$POSTVAR["product_description_index"] = isset($POSTVAR["product_description_index"]) ? $POSTVAR["product_description_index"] : 0;
				
				$query = "UPDATE kpulse_products SET product_name='". addslashes($POSTVAR["product_name"]) ."', 
															product_code_number='". addslashes($POSTVAR["product_code_number"]) ."',
															product_producer_id=". $POSTVAR["product_producer_id"] .", 
															product_publicate=". $POSTVAR["product_publicate"] .", 
															product_recomended=". $POSTVAR["product_recomended"] .", 
															product_cat_id=". $POSTVAR["product_cat_id"] .",
															product_admin_rating=". $POSTVAR["product_admin_rating"] .", 
															product_price=". $POSTVAR["product_price"] .", 
															product_description='". addslashes($POSTVAR["product_description"]) ."',
															product_description_index=". $POSTVAR["product_description_index"] .",
															product_short_des='". addslashes($POSTVAR["product_short_des"]) ."',
															product_keywords='". addslashes($POSTVAR["product_keywords"]) ."',
															product_page_des='". addslashes($POSTVAR["product_page_des"]) ."',
															product_creator_id=". $_SESSION["user_id"] .",
														  	product_created=". time() .",
														  	product_editor_id=". $_SESSION["user_id"] .",
														  	product_edited=". time() ."
														  	WHERE product_id=". $_GET["element"]; 
				
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				
				//SETTING PHOTOS
				foreach($POSTVAR["photo"] as $key => $value){
					if($POSTVAR["defimage"] == $key){
						$catdefault = "1";
					}
					else{
						$catdefault = "0";
					}
					$query = "UPDATE kpulse_photos SET photo_alt='". addslashes($value) ."', photo_cat_logo=". $catdefault ." WHERE photo_id=". $key;
					$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				}
				//adding new photos
				addPhotos($_FILES, $POSTVAR, $_GET["element"]);				
			break;
		}
	}
}

function calculateContentPulseAdd(){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		$content = displayFormTemplatePulse($_GET["type"]);
		return $content;
	}
}

/*templating function for all form-types*/
function displayFormTemplatePulse($type, $resources = null){
	if(isset($type) && $type!=""){
		switch($type){
			case "products":
				if(isset($resources["creator"])){
					?>
					<div>
					<ul>
						<li>Создал: <?php echo $resources["creator"] ?></li>
						<li>Время создания: <?php echo date("F j, Y, g:i a", $resources["product_created"]) ?></li>
						<li>Отредактировал: <?php echo $resources["editor"] ?></li>
						<li>Время реадктирования: <?php echo date("F j, Y, g:i a", $resources["product_edited"]) ?></li>
					</ul>				
					</div>
					<?php 
				}
				?>
				<form id="templateform" method="post" enctype="multipart/form-data" action="<?php echo domain_path ."index.php?route=kse_admin/". ((isset($resources["product_id"])) ? "pulseedit/&element=". $resources["product_id"] : "pulselist/add/") ."&type=". $type ?>">
				<label for="product_name">Название Продукта</label><input id="product_name" name="product_name" value="<?php echo $resources["product_name"] ?>" />
				<label for="product_code_number">Артикул</label><input id="product_code_number" name="product_code_number" value="<?php echo $resources["product_code_number"] ?>" />
				<label for="product_producer_id">Производитель</label>
					<select id="product_producer_id" name="product_producer_id">
					<?php 
						$query = "SELECT producer_id, producer_name FROM kpulse_producers";
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
							if($row["producer_id"] === $resources["product_producer_id"]){
								echo "<option value=\"". $row["producer_id"] ."\" selected>". $row["producer_name"] ."</option>";
							}
							else{
								echo "<option value=\"". $row["producer_id"] ."\">". $row["producer_name"] ."</option>";
							}
						}
					?>	
					</select>
				<fieldset>
					<legend>Опции Товара</legend>
					<label for="product_publicate">Публикация</label>
					<?php
						echo $resources["product_publicate"]  ? "<input type=\"checkbox\" id=\"product_publicate\" name=\"product_publicate\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"product_publicate\" name=\"product_publicate\" value=\"1\">"; 
					?>
					<label for="product_recomended">Рекомендованый товар</label>
					<?php
						echo $resources["product_recomended"]  ? "<input type=\"checkbox\" id=\"product_recomended\" name=\"product_recomended\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"product_recomended\" name=\"product_recomended\" value=\"1\">"; 
					?>
					<label for="product_description_index">Индексация Описания</label>
					<?php
						echo $resources["product_description_index"]  ? "<input type=\"checkbox\" id=\"product_description_index\" name=\"product_description_index\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"product_description_index\" name=\"product_description_index\" value=\"1\">"; 
					?>
					
				</fieldset>
				
				<label for="product_cat_id">Категория</label>
					<select id="product_cat_id" name="product_cat_id">
					<?php 
						$query = "SELECT cat_id, cat_name, (SELECT cat_name FROM kpulse_categories WHERE cat_lft < t1.cat_lft AND cat_rgt > t1.cat_rgt AND cat_id > 1 ORDER BY cat_lft LIMIT 1 ) as parent FROM kpulse_categories as t1";
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
							if($row["cat_id"] === $resources["product_cat_id"]){
								echo "<option value=\"". $row["cat_id"] ."\" selected>". $row["cat_name"] ."(". $row["parent"] .")</option>";
							}
							else{
								echo "<option value=\"". $row["cat_id"] ."\">". $row["cat_name"] ."(". $row["parent"] .")</option>";
							}
						}
					?>	
					</select>
				<label for="product_admin_rating">Рейтинг администратора</label><input id="product_admin_rating" name="product_admin_rating" value="<?php echo isset($resources["product_admin_rating"]) ? $resources["product_admin_rating"] : 0  ?>" />
				<label for="product_price">Цена(в гривнах)</label><input id="product_price" name="product_price" value="<?php echo isset($resources["product_price"]) ? $resources["product_price"] : 0 ?>" />
				<fieldset id="photos">
				<legend>Картинки</legend>
					<?php
						if(isset($resources["product_id"])){//check if we adding product or creating new
							$query = "SELECT photo_id, photo_tiny_img, photo_cat_logo, photo_alt FROM kpulse_photos WHERE photo_product_id=". $resources["product_id"];
							$result = mysql_query($query) or die(mysql_error());
							while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
								echo $row["photo_cat_logo"] ? "<div><input type=\"radio\" name=\"defimage\" value=\"". $row["photo_id"] ."\" checked=\"checked\" />" : "<div><input type=\"radio\" name=\"defimage\" value=\"". $row["photo_id"] ."\" />";
								echo "<img src=\"images/product_images/tiny/". $row["photo_tiny_img"] ."\"><input id=\"photo-". $row["photo_id"] ."\" name=\"photo[". $row["photo_id"] ."]\" type=\"text\" value=\"". $row["photo_alt"] ."\"></div>";
							}
						}
					?>
				<input type="file" id="addimage" name="browse">
				</fieldset>
				<label for="product_description">Описание</label><textarea id="product_description" name="product_description" rows="6" cols="90"><?php echo $resources["product_description"] ?></textarea>
				<label for="product_short_des">Краткое описание</label><textarea id="product_short_des" name="product_short_des" rows="3" cols="50"><?php echo $resources["product_short_des"] ?></textarea>
				<label for="product_keywords">Meta keywords</label><textarea id="product_keywords" name="product_keywords" rows="3" cols="50"><?php echo $resources["product_keywords"] ?></textarea>
				<label for="product_page_des">Meta description</label><textarea id="product_page_des" name="product_page_des" rows="3" cols="50"><?php echo $resources["product_page_des"] ?></textarea>
				<?php
			break;
			case "categories":
				if(isset($resources["creator"])){
					?>
					<div>
					<ul>
						<li>Создал: <?php echo $resources["creator"] ?></li>
						<li>Время создания: <?php echo date("F j, Y, g:i a", $resources["cat_created"]) ?></li>
						<li>Отредактировал: <?php echo $resources["editor"] ?></li>
						<li>Время реадктирования: <?php echo date("F j, Y, g:i a", $resources["cat_edited"]) ?></li>
					</ul>				
					</div>
					<?php 
				}
				?>
				<form id="templateform" method="post" enctype="multipart/form-data" action="<?php echo domain_path ."index.php?route=kse_admin/". ((isset($resources["cat_id"])) ? "pulseedit/&element=". $resources["cat_id"] : "pulselist/add/") ."&type=". $type ?>">
				<label for="cat_name">Название Категории</label><input id="cat_name" name="cat_name" value="<?php echo $resources["cat_name"] ?>" />
				<label for="cat_title">Название Ссылки</label><input id="cat_title" name="cat_title" value="<?php echo $resources["cat_title"] ?>" />
				<label for="cat_link">Ссылка на категорию</label><input id="cat_link" name="cat_link" value="<?php echo $resources["cat_link"] ?>" />
				<label for="cat_keywords">Meta keywords</label><textarea id="cat_keywords" name="cat_keywords" rows="3" cols="50"><?php echo $resources["cat_keywords"] ?></textarea>
				<label for="cat_description">Краткое описание</label><textarea id="cat_description" name="cat_description" rows="3" cols="50"><?php echo $resources["cat_description"] ?></textarea>
				<fieldset>
				<legend>Настройки категорий</legend>
				<label for="cat_visible">Публикация</label>
					<?php
						echo $resources["cat_visible"]  ? "<input type=\"checkbox\" id=\"cat_visible\" name=\"cat_visible\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"cat_visible\" name=\"cat_visible\" value=\"1\">"; 
					?>	
				</fieldset>	
				<label for="cat_page_des">Дополнительное описание</label><textarea id="cat_page_des" name="cat_page_des" rows="3" cols="50"><?php echo $resources["cat_page_des"] ?></textarea>
				
				<?php
			break;
			case "producers":
				if(isset($resources["creator"])){
					?>
					<div>
					<ul>
						<li>Создал: <?php echo $resources["creator"] ?></li>
						<li>Время создания: <?php echo date("F j, Y, g:i a", $resources["producer_created"]) ?></li>
						<li>Отредактировал: <?php echo $resources["editor"] ?></li>
						<li>Время реадктирования: <?php echo date("F j, Y, g:i a", $resources["producer_edited"]) ?></li>
					</ul>				
					</div>
					<?php 
				}
				?>
				<form id="templateform" method="post" enctype="multipart/form-data" action="<?php echo domain_path ."index.php?route=kse_admin/". ((isset($resources["producer_id"])) ? "pulseedit/&element=". $resources["producer_id"] : "pulselist/add/") ."&type=". $type ?>">
				<label for="producer_name">Название Производителя</label><input id="producer_name" name="producer_name" value="<?php echo $resources["producer_name"] ?>" />
				<label for="producer_link">Ссылка на производителя</label><input id="producer_link" name="producer_link" value="<?php echo $resources["producer_link"] ?>" />
				<label for="producer_keywords">Meta keywords</label><textarea id="producer_keywords" name="producer_keywords" rows="3" cols="50"><?php echo $resources["producer_keywords"] ?></textarea>
				<label for="producer_description">Краткое описание</label><textarea id="producer_description" name="producer_description" rows="3" cols="50"><?php echo $resources["producer_description"] ?></textarea>
				<label for="producer_page_des">Дополнительное описание</label><textarea id="producer_page_des" name="producer_page_des" rows="3" cols="50"><?php echo $resources["producer_page_des"] ?></textarea> 
				<?php
			break;
		}
		?>
		<input id="submitbutton" value="Edit" type="Submit">
		</form>
		<?php
	}
}

function addPhotos($photoaaray, $POSTVAR, $productid){
	foreach($photoaaray as $key => $imgvalue){
		if($imgvalue["error"]==0 && $imgvalue["type"]=="image/jpeg"){
			$result=mysql_query("SELECT max(photo_id)+1 FROM kpulse_photos");
			$res=mysql_fetch_assoc($result);
			$photo_id=$res["max(photo_id)+1"]?$res["max(photo_id)+1"]:1;
			$img=md5($photo_id.$_SERVER['REMOTE_ADDR']).".jpg";
			copy($imgvalue['tmp_name'], real_path ."images/product_images/large/".$img);
			
			image_resizer($imgvalue['tmp_name'], real_path ."images/product_images/tiny/".$img, 100, 0);
			image_resizer($imgvalue['tmp_name'], real_path ."images/product_images/small/".$img, 350, 0);
			
			
			unlink($imgvalue['tmp_name']);
			
			if($POSTVAR["defimage"] == $key){
						$catdefault = "1";
					}
					else{
						$catdefault = "0";
					}
					$query = "INSERT INTO kpulse_photos SET photo_product_id=". $productid .",
															photo_large_img='". $img ."',
															photo_small_img='". $img ."',
															photo_tiny_img='". $img ."',
															photo_alt='". addslashes($POSTVAR["addphoto"][$key]) ."', photo_cat_logo=". $catdefault;
					$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
			
		}	
	}
}

//old pulse resizer
function image_resizer($tmp_name, $name, $width, $height){
	$im_data=@getimagesize($tmp_name);
	if ($im_data[2]==1 || $im_data[2]==2 || $im_data[2]==3) 
	{
  		include_once(real_path ."kse". DIRSEP ."modules". DIRSEP ."kpulse". DIRSEP ."class.img2thrumb.php");
		if($width!=0)
		{
			if($height==0)
				$height=floor($im_data[1]*$width/$im_data[0]);
			else
			{
				if((($width>$height)&&($im_data[1]>$im_data[0]))||(($width<$height)&&($im_data[1]<$im_data[0])))
				{
					$tmp=$width;
					$width=$height;
					$height=$tmp;
				}
			}
			$Img2Thumb = new Img2Thumb($tmp_name, $width,  $height, $name, 0); 
		}
		else
			@copy($tmp_name,$name);			    
	}
}
?>